<?php
declare(strict_types = 1);

namespace app\index\service;

use think\facade\Session;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Db;
use think\admin\Service as AdminService;
//use think\Cache as ca;

class ConfigService extends AdminService
{

    //初始化配置
    protected $table = 'Md5';//表名

    // 初始化
    protected function initialize()
    {
        //$this->config = config('excel');//调用配置文件示例
    }

    /**
    * 保存配置
    * @author    saruri <saruri@163.com>
    * @date      2021/02/20 17:23:30
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function save($data)
    {
        //do somehing
        //return $this->check($data);
        $a=$this->check($data);
        //halt($a);
        if ($a===false) {
            //return true;
            return data_save($this->table, $data, 'id');
        }
        // return '已经有数据';
        return false;
        //exit("这里是:");
    }


    /**
    * 检测重复
    * @author    saruri <saruri@163.com>
    * @date      2021/02/20 17:23:51
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function check($data)
    {
        //do somehing
        $tableField="*";
        $where[]=['md5','=',$data['md5']];
        $where[]=['content','=',$data['content']];
        //$data['create_time']=time();
        
        $query = $this->app->db->name($this->table)->where($where)->find() > 0;
        //return $this->app->db->getLastsql();
        return $query;
    }


    /**
    * 获取配置列表 根据表头
    * @author    saruri <saruri@163.com>
    * @date      2021/02/22 07:31:01
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function getList(string $head='')
    {
        //do somehing
        //return 'zhelishi list';
        $tableField="*";
        if ($head=='') {
            return false;
        }

        $where[]=['head','=',$head];
        //$data['create_time']=time();
        
        $query = $this->app->db->name($this->table)->where($where)->select();
        $data['query']=$query;
        $data['sql']=$this->app->db->getLastSql();
        //return  $data;
        return  $data['query'];
    }


    /**
    * 保存路径mdd5
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 14:32:16
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function saveMd5($arr)
    {
        //do somehing
        $arr['create_time']=time();
        return data_save('files', $arr, 'id');
    }

    /**
    * 获取文件路径
    * @author    saruri <saruri@163.com>
    * @date      2021/03/04 12:00:11  
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */ 
    public function  getFilePath($md5)
    {
        //do somehing
        //exit("这里是:");\
        $table='files';
        $tableField="*";
        $where[]=['md5','=',$md5];
        
        //$data['create_time']=time();
        //$query = Db::table($table)->cache(false)->where($where)->find();
        $query = $this->app->db->name($table)->where($where)->find();
        //return $this->app->db->getLastsql();
        return $query;
         
    }


    /**
    * 方法说明 构造分页和百分比
    * @author    saruri <saruri@163.com>
    * @date      2021/03/02 10:11:59
    * @auth false
    * @menu false
    * @login false
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */
    public function buildPage($arr)
    {
        //do somehing
        //exit("这里是:");

        return $arr;
    }
}
